var moment = require('moment');
var _ = require('underscore');
var superagent = require('superagent');

module.exports = function (app) {

  app.get(/^\/mobile\/sd\/\d+\/api\/property\/user\/properties/, function (req, res) {
    var index = req.originalUrl.indexOf('/api');
    var cuttedUrl = req.originalUrl.substr(index + 5);
    var url = 'http://10.128.6.237:8007/api/' + cuttedUrl;
    superagent
      .get(url)
      .set('User-Agent', req.headers['user-agent'])
      .set('cookie', 'sensorsdata-username=zhangSteven; sensorsdata-token=4d64fbb11775ef09f47184ab593b8fbb3db7fb947874b590692ce0aba2b30f4cc392f8508cbe4402973f043a8d3c560e89aab7eafe4bf264e615cf0cd079b906; sensorsdata-role=0; sensorsdata-userid=1237; _ga=GA1.2.592458834.1461649678; _gat=1')
      .set('Accept', 'application/json')
      .set('sensorsdata-token', '4d64fbb11775ef09f47184ab593b8fbb3db7fb947874b590692ce0aba2b30f4cc392f8508cbe4402973f043a8d3c560e89aab7eafe4bf264e615cf0cd079b906')
      .set('Host', 'golddemo.cloud.sensorsdata.cn')
      .end(function (err, innerRes) {
        if (err) {
          res.status(err.status).end(err.message);
        } else {
          res.status(innerRes.status).json(innerRes.body);
        }
      });
  });

  app.post(/^\/mobile\/sd\/\d+\/api\/property\/user\/properties/, function (req, res) {
    var index = req.originalUrl.indexOf('/api');
    var cuttedUrl = req.originalUrl.substr(index + 5);
    var url = 'http://10.128.6.237:8007/api/' + cuttedUrl;
    superagent
      .post(url)
      .set('User-Agent', req.headers['user-agent'])
      .set('cookie', 'sensorsdata-username=zhangSteven; sensorsdata-token=4d64fbb11775ef09f47184ab593b8fbb3db7fb947874b590692ce0aba2b30f4cc392f8508cbe4402973f043a8d3c560e89aab7eafe4bf264e615cf0cd079b906; sensorsdata-role=0; sensorsdata-userid=1237; _ga=GA1.2.592458834.1461649678; _gat=1')
      .set('Accept', 'application/json')
      .set('Content-Type', 'application/json')
      .set('sensorsdata-token', '4d64fbb11775ef09f47184ab593b8fbb3db7fb947874b590692ce0aba2b30f4cc392f8508cbe4402973f043a8d3c560e89aab7eafe4bf264e615cf0cd079b906')
      .set('Host', 'golddemo.cloud.sensorsdata.cn')
      .send(req.body)
      .end(function (err, innerRes) {
        if (err) {
          res.status(err.status).end(err.message);
        } else {
          res.status(innerRes.status).json(innerRes.body);
        }
      });
  });

  app.post('/mobile/sd/:appId/api/events/user/list/csv', function (req, res) {
    var index = req.originalUrl.indexOf('/api');
    var cuttedUrl = req.originalUrl.substr(index + 5);
    var url = 'http://10.128.6.237:8007/api/' + cuttedUrl;
    superagent
      .post(url)
      .set('User-Agent', req.headers['user-agent'])
      .set('cookie', 'sensorsdata-username=zhangSteven; sensorsdata-token=4d64fbb11775ef09f47184ab593b8fbb3db7fb947874b590692ce0aba2b30f4cc392f8508cbe4402973f043a8d3c560e89aab7eafe4bf264e615cf0cd079b906; sensorsdata-role=0; sensorsdata-userid=1237; _ga=GA1.2.592458834.1461649678; _gat=1')
      .set('Accept', req.headers.accept)
      .set('Content-Type', req.headers['content-type'])
      .set('sensorsdata-token', '4d64fbb11775ef09f47184ab593b8fbb3db7fb947874b590692ce0aba2b30f4cc392f8508cbe4402973f043a8d3c560e89aab7eafe4bf264e615cf0cd079b906')
      .set('Host', 'golddemo.cloud.sensorsdata.cn')
      .send(req.body)
      .buffer(false)
      .end()
      .req
      .on('response', function(innerRes) {
        console.log('Content-Disposition', innerRes.headers['content-disposition']);
        res.append('Content-Disposition', innerRes.headers['content-disposition']);
        res.append('Content-Type', innerRes.headers['content-type'])
        innerRes.pipe(res);
      });
  });

  app.post(/^\/mobile\/sd\/\d+\/api\/users\/list/, function (req, res) {
    var index = req.originalUrl.indexOf('/api');
    var cuttedUrl = req.originalUrl.substr(index + 5);
    var url = 'http://10.128.6.237:8007/api/' + cuttedUrl;
    superagent
      .post(url)
      .set('User-Agent', req.headers['user-agent'])
      .set('cookie', 'sensorsdata-username=zhangSteven; sensorsdata-token=4d64fbb11775ef09f47184ab593b8fbb3db7fb947874b590692ce0aba2b30f4cc392f8508cbe4402973f043a8d3c560e89aab7eafe4bf264e615cf0cd079b906; sensorsdata-role=0; sensorsdata-userid=1237; _ga=GA1.2.592458834.1461649678; _gat=1')
      .set('Accept', 'application/json')
      .set('Content-Type', 'application/json')
      .set('sensorsdata-token', '4d64fbb11775ef09f47184ab593b8fbb3db7fb947874b590692ce0aba2b30f4cc392f8508cbe4402973f043a8d3c560e89aab7eafe4bf264e615cf0cd079b906')
      .set('Host', 'golddemo.cloud.sensorsdata.cn')
      .send(req.body)
      .end(function (err, innerRes) {
        if (err) {
          res.status(err.status).end(err.message);
        } else {
          res.status(innerRes.status).json(innerRes.body);
        }
      });
  });

  app.post('/mobile/sd/:appId/api/events/sequence/csv', function (req, res) {
    var index = req.originalUrl.indexOf('/api');
    var cuttedUrl = req.originalUrl.substr(index + 5);
    var url = 'http://10.128.6.237:8007/api/' + cuttedUrl;
    superagent
      .post(url)
      .set('User-Agent', req.headers['user-agent'])
      .set('cookie', 'sensorsdata-username=zhangSteven; sensorsdata-token=4d64fbb11775ef09f47184ab593b8fbb3db7fb947874b590692ce0aba2b30f4cc392f8508cbe4402973f043a8d3c560e89aab7eafe4bf264e615cf0cd079b906; sensorsdata-role=0; sensorsdata-userid=1237; _ga=GA1.2.592458834.1461649678; _gat=1')
      .set('Accept', req.headers.accept)
      .set('Content-Type', req.headers['content-type'])
      .set('sensorsdata-token', '4d64fbb11775ef09f47184ab593b8fbb3db7fb947874b590692ce0aba2b30f4cc392f8508cbe4402973f043a8d3c560e89aab7eafe4bf264e615cf0cd079b906')
      .set('Host', 'golddemo.cloud.sensorsdata.cn')
      .send(req.body)
      .buffer(false)
      .end()
      .req
      .on('response', function(innerRes) {
        console.log('Content-Disposition', innerRes.headers['content-disposition']);
        res.append('Content-Disposition', innerRes.headers['content-disposition']);
        res.append('Content-Type', innerRes.headers['content-type'])
        innerRes.pipe(res);
      });
  });

  app.post(/^\/mobile\/sd\/\d+\/api\/events\/sequence/, function (req, res) {
    var index = req.originalUrl.indexOf('/api');
    var cuttedUrl = req.originalUrl.substr(index + 5);
    var url = 'http://10.128.6.237:8007/api/' + cuttedUrl;
    superagent
      .post(url)
      .set('User-Agent', req.headers['user-agent'])
      .set('cookie', 'sensorsdata-username=zhangSteven; sensorsdata-token=4d64fbb11775ef09f47184ab593b8fbb3db7fb947874b590692ce0aba2b30f4cc392f8508cbe4402973f043a8d3c560e89aab7eafe4bf264e615cf0cd079b906; sensorsdata-role=0; sensorsdata-userid=1237; _ga=GA1.2.592458834.1461649678; _gat=1')
      .set('Accept', 'application/json')
      .set('Content-Type', 'application/json')
      .set('sensorsdata-token', '4d64fbb11775ef09f47184ab593b8fbb3db7fb947874b590692ce0aba2b30f4cc392f8508cbe4402973f043a8d3c560e89aab7eafe4bf264e615cf0cd079b906')
      .set('Host', 'golddemo.cloud.sensorsdata.cn')
      .send(req.body)
      .end(function (err, innerRes) {
        if (err) {
          res.status(err.status).end(err.message);
        } else {
          res.status(innerRes.status).json(innerRes.body);
        }
      });
  });

  app.get(/^\/mobile\/sd\/\d+\/api/, function (req, res) {
    var index = req.originalUrl.indexOf('/api');
    var cuttedUrl = req.originalUrl.substr(index + 5);
    var url = 'http://10.128.6.237:8007/api/' + cuttedUrl;
    superagent
      .get(url)
      .set('User-Agent', req.headers['user-agent'])
      .set('cookie', 'sensorsdata-username=zhangSteven; sensorsdata-token=4d64fbb11775ef09f47184ab593b8fbb3db7fb947874b590692ce0aba2b30f4cc392f8508cbe4402973f043a8d3c560e89aab7eafe4bf264e615cf0cd079b906; sensorsdata-role=0; sensorsdata-userid=1237; _ga=GA1.2.592458834.1461649678; _gat=1')
      .set('Accept', 'application/json')
      .set('sensorsdata-token', '4d64fbb11775ef09f47184ab593b8fbb3db7fb947874b590692ce0aba2b30f4cc392f8508cbe4402973f043a8d3c560e89aab7eafe4bf264e615cf0cd079b906')
      .set('Host', 'golddemo.cloud.sensorsdata.cn')
      .end(function (err, innerRes) {
        if (err) {
          res.status(err.status).end(err.message);
        } else {
          res.status(innerRes.status).json(innerRes.body);
        }
      });
  });

  app.post('/mobile/sd/:appId/api/events/report/csv', function (req, res) {
    var index = req.originalUrl.indexOf('/api');
    var cuttedUrl = req.originalUrl.substr(index + 5);
    var url = 'http://10.128.6.237:8007/api/' + cuttedUrl;
    superagent
      .post(url)
      .set('User-Agent', req.headers['user-agent'])
      .set('cookie', 'sensorsdata-username=zhangSteven; sensorsdata-token=4d64fbb11775ef09f47184ab593b8fbb3db7fb947874b590692ce0aba2b30f4cc392f8508cbe4402973f043a8d3c560e89aab7eafe4bf264e615cf0cd079b906; sensorsdata-role=0; sensorsdata-userid=1237; _ga=GA1.2.592458834.1461649678; _gat=1')
      .set('Accept', req.headers.accept)
      .set('Content-Type', req.headers['content-type'])
      .set('sensorsdata-token', '4d64fbb11775ef09f47184ab593b8fbb3db7fb947874b590692ce0aba2b30f4cc392f8508cbe4402973f043a8d3c560e89aab7eafe4bf264e615cf0cd079b906')
      .set('Host', 'golddemo.cloud.sensorsdata.cn')
      .send(req.body)
      .buffer(false)
      .end()
      .req
      .on('response', function(innerRes) {
        console.log('Content-Disposition', innerRes.headers['content-disposition']);
        res.append('Content-Disposition', innerRes.headers['content-disposition']);
        res.append('Content-Type', innerRes.headers['content-type'])
        innerRes.pipe(res);
      });
  });

  app.post(/^\/mobile\/sd\/\d+\/api/, function (req, res) {
    var index = req.originalUrl.indexOf('/api');
    var cuttedUrl = req.originalUrl.substr(index + 5);
    var url = 'http://10.128.6.237:8007/api/' + cuttedUrl;
    superagent
      .post(url)
      .set('User-Agent', req.headers['user-agent'])
      .set('cookie', 'sensorsdata-username=zhangSteven; sensorsdata-token=4d64fbb11775ef09f47184ab593b8fbb3db7fb947874b590692ce0aba2b30f4cc392f8508cbe4402973f043a8d3c560e89aab7eafe4bf264e615cf0cd079b906; sensorsdata-role=0; sensorsdata-userid=1237; _ga=GA1.2.592458834.1461649678; _gat=1')
      .set('Accept', 'application/json')
      .set('Content-Type', 'application/json')
      .set('sensorsdata-token', '4d64fbb11775ef09f47184ab593b8fbb3db7fb947874b590692ce0aba2b30f4cc392f8508cbe4402973f043a8d3c560e89aab7eafe4bf264e615cf0cd079b906')
      .set('Host', 'golddemo.cloud.sensorsdata.cn')
      .send(req.body)
      .end(function (err, innerRes) {
        if (err) {
          res.status(err.status).end(err.message);
        } else {
          res.status(innerRes.status).json(innerRes.body);
        }
      });
  });

  app.delete(/^\/mobile\/sd\/\d+\/api/, function (req, res) {
    var index = req.originalUrl.indexOf('/api');
    var cuttedUrl = req.originalUrl.substr(index + 5);
    var url = 'http://10.128.6.237:8007/api/' + cuttedUrl;
    superagent
      .del(url)
      .set('User-Agent', req.headers['user-agent'])
      .set('cookie', 'sensorsdata-username=zhangSteven; sensorsdata-token=4d64fbb11775ef09f47184ab593b8fbb3db7fb947874b590692ce0aba2b30f4cc392f8508cbe4402973f043a8d3c560e89aab7eafe4bf264e615cf0cd079b906; sensorsdata-role=0; sensorsdata-userid=1237; _ga=GA1.2.592458834.1461649678; _gat=1')
      .set('Accept', 'application/json')
      .set('sensorsdata-token', '4d64fbb11775ef09f47184ab593b8fbb3db7fb947874b590692ce0aba2b30f4cc392f8508cbe4402973f043a8d3c560e89aab7eafe4bf264e615cf0cd079b906')
      .set('Host', 'golddemo.cloud.sensorsdata.cn')
      .end(function (err, innerRes) {
        if (err) {
          res.status(err.status).end(err.message);
        } else {
          res.status(innerRes.status).json(innerRes.body);
        }
      });
  });
}
